clear all;clc
addpath(genpath('.'));  %添加工具箱路径
%% 超参数设置
% ZOOM=512;%暂定512*512为一个分区
N=256;%暂定每个分区中的块大小为：256*256分块

%% 读入图像
Cover=rgb2ycbcr(imread('./image/Big_image.jpg'));
Cover=imcrop(Cover,[1,1,2559,1535]);
% [DFT_Cover,P]=InputProc(Cover);%取幅值谱
% Cover=imresize(Cover,2);
Y_Cover=Cover(:,:,1);
[w,h]=size(Y_Cover);

%% 秘密信息生成
key=rng; %%key为伪随机序列种子
msg = num2str(uint8(rand(1,w*h)));
msg=strrep(msg,' ','');%剔除空格
%% 可以构造一个 msg
msg='101010101010101010101010101010101010101010101010101010101010101010101010';
msg=repmat(msg,1,5);
p=1;    %指向msg的指针
%% 秘密信息生成--注：仅仅生成35bit
[emd_msg,p] = get_msg(msg,p,35);%获取秘密信息,一次取35位，前11位走BCH(31,11),后24位走BCH(63,24)
%进行BCH编码
% msg_A=repmat(My_BCHenc(emd_msg(1:11),31,11),1,2);
% msg_B=My_BCHenc(emd_msg(12:end),63,24);
%% 嵌入
for i=1:w/N
    for j=1:h/N
        [emd_msg,p] = get_msg(msg,p,1);%每次嵌入一位
        Cover_block=Y_Cover((i-1)*N+1:i*N,(j-1)*N+1:j*N);
        if strcmp(emd_msg,'0') %则山宽为128*128
            sub_Cover_block=Cover_block(64:191,64:191);
            [sub_DFT_block,P]=InputProc(sub_Cover_block);%取幅值谱
            Stego_DFT_block=Generate_correspon_A(sub_DFT_block);
            sub_Stego_block=OutputProc(Stego_DFT_block,P);
            Cover_block(64:191,64:191)=sub_Stego_block;
            Stego_block=Cover_block;
        else% emd_msg==1 则山宽为256*256
            [DFT_block,P]=InputProc(Cover_block);%取幅值谱
            Stego_DFT_block=Generate_correspon_B(DFT_block);
            Stego_block=OutputProc(Stego_DFT_block,P);
        end
        
        Y_stego((i-1)*N+1:i*N,(j-1)*N+1:j*N)=Stego_block;
    end
end
%% 转回RGB
find_psnr(Y_stego,Y_Cover)
Stego=cat(3,Y_stego,Cover(:,:,2),Cover(:,:,3));
Stego=ycbcr2rgb(Stego);
%% 存起来
imwrite(Stego,'./Marked_image/Width_Marked_fruits7.png');
save Stego Stego
save p p
save key key


% 前两层For循环为了获取区域，后两层For循环为了嵌入
% for I=1:w/ZOOM
%     for J=1:h/ZOOM
%           C_ZOOM=Y_Cover((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM);%取出这个ZOOM
%           [sub_w,sub_h]=size(C_ZOOM);
%         for i=1:sub_w/N 
%            for j=1:sub_h/N
%                % 取块
%                 Cover_block=C_ZOOM((i-1)*N+1:i*N,(j-1)*N+1:j*N);
%                 [DFT_block,P]=InputProc(Cover_block);%取幅值谱
%                 if i==1 && j==1 %确定为同步块
%                     Stego_DFT_block=Generate_correspon(DFT_block);
%                     Stego_block=OutputProc(Stego_DFT_block,P);
%                 else
%                     if i==1&& j==2 %嵌msg_A
%                     Stego_block=Embed_watermark(Cover_block,msg_A);
%                     else %嵌msg_B
%                     Stego_block=Embed_watermark(Cover_block,msg_B);
%                     end
%                 end
%                 %imshow(uint8(Stego_DFT_block/256))
%                 Y_stego((i-1)*N+1:i*N,(j-1)*N+1:j*N)=Stego_block;
%            end
%         end
%         Y_stego_ZOOM((I-1)*ZOOM+1:I*ZOOM,(J-1)*ZOOM+1:J*ZOOM)=Y_stego;
%     end
% end

% find_psnr(Y_stego_ZOOM,Y_Cover)
% %% 转回RGB
% Stego=cat(3,Y_stego_ZOOM,Cover(:,:,2),Cover(:,:,3));
% Stego=ycbcr2rgb(Stego);
% %% 存起来
% imwrite(Stego,'./Marked_image/Width_Marked_fruits.png');
% save Stego Stego
% save p p
% save key key